package com.acmedcare.framework.exchange.center.endpoint;

import com.acmedcare.framework.exchange.center.common.Page;
import com.acmedcare.framework.exchange.center.endpoint.service.TaskHistoryService;
import com.acmedcare.framework.exchange.center.entity.bean.TaskDetail;
import com.acmedcare.framework.exchange.center.entity.dto.TaskHistoryQueryDto;
import com.acmedcare.framework.exchange.center.entity.dto.TaskHistoryViewDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "任务历史")
@RequestMapping("/task-histories")
public class TaskHistory {

  private final TaskHistoryService historyService;

  public TaskHistory(
      TaskHistoryService historyService) {
    this.historyService = historyService;
  }

  @ApiOperation("获取历史列表")
  @GetMapping("/list")
  public ResponseEntity<Page<TaskHistoryViewDto>> getList(TaskHistoryQueryDto taskHistoryQueryDto) {

    Page<TaskHistoryViewDto> viewDtoPage = historyService.getList(taskHistoryQueryDto);
    if (viewDtoPage == null) {

      return ResponseEntity.noContent().build();
    }

    return ResponseEntity.ok(viewDtoPage);
  }

  @ApiOperation("获取任务详情")
  @GetMapping("/{taskDetailId}")
  public ResponseEntity<TaskDetail> getTaskDetail(
      @PathVariable("taskDetailId") @ApiParam(value = "任务详情id",required = true) Long taskDetailId) {
    TaskDetail detail = historyService.getDetail(taskDetailId);

    if (detail == null) {

      return ResponseEntity.noContent().build();
    }

    return ResponseEntity.ok(detail);
  }
}